package cz.proks.easyhelpdesk.gwt.ui.widget;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.easyhelpdesk.gwt.event.HasLabelChangeHandlers;
import cz.proks.easyhelpdesk.gwt.event.LabelChangeEvent;
import cz.proks.easyhelpdesk.gwt.event.LabelChangeEvent.Handler;

public class DisclosureLabel extends Composite implements HasLabelChangeHandlers {

    private static DisclosureLabelUiBinder uiBinder = GWT.create(DisclosureLabelUiBinder.class);

    interface DisclosureLabelUiBinder extends UiBinder<Widget, DisclosureLabel> {
    }          

    @UiField Label header;
    @UiField DisclosurePanel disclosurePanel;
    @UiField FlowPanel panel;
    @UiField FlowPanel contentPanel;
        
    private final List<Label> labels = new LinkedList<Label>();    
    
    public DisclosureLabel() {
        initWidget(uiBinder.createAndBindUi(this));
    }
    
    @Override
    protected void initWidget(Widget widget) {        
        super.initWidget(widget);
        
        panel.setVisible(false);
                
        header.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                disclosurePanel.setOpen(!disclosurePanel.isOpen());                
            }
        });
        
        disclosurePanel.addOpenHandler(new OpenHandler<DisclosurePanel>() {
            
            @Override
            public void onOpen(OpenEvent<DisclosurePanel> event) {
                panel.setVisible(true);
            }
        });
        
        disclosurePanel.addCloseHandler(new CloseHandler<DisclosurePanel>() {
            
            @Override
            public void onClose(CloseEvent<DisclosurePanel> event) {
                panel.setVisible(false);                
            }
        });                
    }
    
    @Override
    public HandlerRegistration addLabelChangeHandler(Handler handler) {
        return addHandler(handler, LabelChangeEvent.TYPE);
    }
    
    public String getHeaderText() {
        return header.getText();        
    }
    
    public void setHeaderText(String text) {
        header.setText(text);
    }
    
    public List<Label> getLabels() {
        return labels;
    }
        
    public void setLabels(Collection<Label> labels) {        
        this.labels.clear();
        
        if (labels != null)
            this.labels.addAll(labels);
        
        contentPanel.clear();
        
        for (final Label label : labels) {            
            label.addClickHandler(new ClickHandler() {
                
                @Override
                public void onClick(ClickEvent event) {    
                    event.preventDefault();
                    
                    setHeaderText(label.getText());
                    disclosurePanel.setOpen(false);
                    
                    LabelChangeEvent.fireEvent(DisclosureLabel.this, label);
                }
            });
            
            label.getElement().getStyle().setCursor(Cursor.POINTER);
            contentPanel.add(label);
        }               
    }

}
